//
//  ChildNavView.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/16.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "ChildNavView.h"


@interface ChildNavView()

@property (nonatomic, strong) UIView *trackView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *buttonsArray;
@end



@implementation ChildNavView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self loadSubviewsOnChildNavView];
    }
    return self;
}


- (void)loadSubviewsOnChildNavView{
    UIView *containView = [[UIView alloc]init];
    containView.backgroundColor = [UIColor whiteColor];
    containView.layer.borderColor = KLColor_A1.CGColor;
    containView.layer.borderWidth = 1;
    containView.layer.cornerRadius = 14*HEIGHT;
    containView.layer.masksToBounds = YES;
    self.containerView = containView;
    [self addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(270*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    
    
    UIView *trackView = [[UIView alloc]init];
    trackView.backgroundColor = KLColor_A1;
    trackView.layer.cornerRadius = 14*HEIGHT;
    trackView.layer.masksToBounds = YES;
    [containView addSubview:trackView];
    self.trackView = trackView;
    [trackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.bottom.equalTo(containView);
        make.left.equalTo(containView);
        make.width.mas_equalTo(67.5*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    // 日榜
    UIButton *dayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dayButton setTitle:@"日榜" forState:UIControlStateNormal];
    dayButton.tag = 100;
    [dayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    dayButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [dayButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:dayButton];
    [dayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.left.equalTo(containView);
        make.width.mas_equalTo(67.5*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    [self.buttonsArray addObject:dayButton];
    UIView *seperatorView_day = [[UIView alloc]init];
    seperatorView_day.backgroundColor = KLColor_A1;
    [containView addSubview:seperatorView_day];
    [seperatorView_day mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(dayButton).offset(66*WIDTH);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(10*HEIGHT);
    }];
    
    // 周榜
    UIButton *weekButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [weekButton setTitle:@"周榜" forState:UIControlStateNormal];
    weekButton.tag = 101;
    [weekButton setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    weekButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [weekButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:weekButton];
    [weekButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(dayButton.mas_right);
        make.width.mas_equalTo(67.5*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    [self.buttonsArray addObject:weekButton];
    UIView *seperatorView_week = [[UIView alloc]init];
    seperatorView_week.backgroundColor = KLColor_A1;
    [containView addSubview:seperatorView_week];
    [seperatorView_week mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(weekButton).offset(66*WIDTH);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(10*HEIGHT);
    }];
    
    // 月榜
    UIButton *monthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [monthButton setTitle:@"月榜" forState:UIControlStateNormal];
    monthButton.tag = 102;
    [monthButton setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    monthButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [monthButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:monthButton];
    [monthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(weekButton.mas_right);
        make.width.mas_equalTo(67.5*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    [self.buttonsArray addObject:monthButton];
    UIView *seperatorView_month = [[UIView alloc]init];
    seperatorView_month.backgroundColor = KLColor_A1;
    [containView addSubview:seperatorView_month];
    [seperatorView_month mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(monthButton).offset(66*WIDTH);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(10*HEIGHT);
    }];
    
    // 总榜
    UIButton *finalButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [finalButton setTitle:@"总榜" forState:UIControlStateNormal];
    finalButton.tag = 103;
    [finalButton setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    finalButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [finalButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:finalButton];
    [finalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(containView);
        make.left.equalTo(monthButton.mas_right);
        make.width.mas_equalTo(67.5*WIDTH);
        make.height.mas_equalTo(28*HEIGHT);
    }];
    [self.buttonsArray addObject:finalButton];
    
}

- (void)buttonClick:(UIButton *)sender{
    // 过滤
    [self.buttonsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        [obj setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    }];
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.trackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(67.5*WIDTH * (sender.tag - 100));
    }];
    
    
    if (self.delegateSignal) {
        [self.delegateSignal sendNext:@{@"index":@(sender.tag - 100)}];
    }
}


- (NSMutableArray *)buttonsArray{
    if (!_buttonsArray) {
        _buttonsArray = [NSMutableArray array];
    }
    return _buttonsArray;
}


@end
